package ca.camosun.morsecode;


import android.os.Vibrator;
import android.content.Context;

public class MorseVibrator {
	
	public enum Timing {
	
		DOT_TIME(200),
		DASH_TIME(DOT_TIME.timing() * 3),
		SHORT_PAUSE_TIME(DOT_TIME.timing()),
		LONG_PAUSE_TIME(DOT_TIME.timing() * 7);
		
		private final int timing;
		
		private Timing(int timeInMilliseconds) {
			this.timing = timeInMilliseconds;
		}
		
		private int timing() { return timing; }
		
		public static int returnTiming(MorseDecoder.Token symbol) {
			return MorseVibrator.Timing.valueOf(symbol);
		}

		private static int valueOf(MorseDecoder.Token symbol2) {
			
			switch (symbol2) {
				case DOT:
					return DOT_TIME.timing();
					
				case DASH:
					return DASH_TIME.timing();
					
				case SHORT_PAUSE:
					return SHORT_PAUSE_TIME.timing();
					
				case LONG_PAUSE:
					return LONG_PAUSE_TIME.timing();
					
				default:
					return 0;
			}
		}
	}
	
	private Vibrator mVibrator;

	public MorseVibrator(Context inContext) {
		
		mVibrator = (Vibrator) inContext
				.getSystemService(Context.VIBRATOR_SERVICE);
		
	}
	
	public MorseVibrator() {
		
	}

	public void morseVibrate(MorseDecoder.Token symbol) {

		switch (symbol) {
			case DOT:
			case DASH:
				mVibrator.vibrate(MorseVibrator.Timing.returnTiming(symbol));
				break;
			case SHORT_PAUSE:
			case LONG_PAUSE:
				try {
					Thread.sleep(MorseVibrator.Timing.returnTiming(symbol));
				} catch (InterruptedException e) {
					System.out.println("Couldn't sleep. Error: " + e.getMessage());
				}
				break;
			default:
				break;
		}
	}
}
